package xin.lhuo.we.server.voutils;

import xin.lhuo.we.server.entity.CaiYunSimpleWeather;
import xin.lhuo.we.server.vo.WeatherVo;

import java.util.ArrayList;
import java.util.List;

/**
 * 彩云获取的天气信息转换为需要的信息
 *
 * @author Lhuo
 * @date 2019/5/23 23:43
 */
public class WeatherUtils {
    public static WeatherVo caiYunSimpleWeatherToWeather(CaiYunSimpleWeather simpleWeather,String city) {
        WeatherVo weatherVo = new WeatherVo();
        List<WeatherVo.WeatherBean> weatherBeanList = new ArrayList<>();
        CaiYunSimpleWeather.ResultBean.DailyBean dailyBean = simpleWeather.getResult().getDaily();
        for (int i = 0; i < dailyBean.getSkycon().size(); i++) {
            WeatherVo.WeatherBean weatherBean = new WeatherVo.WeatherBean();
            weatherBean.setSkycon(dailyBean.getSkycon().get(i).getValue());
            weatherBean.setSkyconDesc(skyconToDesc(dailyBean.getSkycon().get(i).getValue()));
            weatherBean.setTemperatureMax(dailyBean.getTemperature().get(i).getMax());
            weatherBean.setTemperatureMin(dailyBean.getTemperature().get(i).getMin());
            weatherBean.setAirQualityDesc(getAirQualityDesc(dailyBean.getAir_quality().getAqi().get(i).getAvg().getChn()));
            weatherBean.setData(dailyBean.getSkycon().get(i).getDate());
            weatherBeanList.add(weatherBean);
        }
        weatherVo.setWeatherBeanList(weatherBeanList);
        weatherVo.setCity(city);
        return weatherVo;
    }

    private static String getAirQualityDesc(Double qirQualityAvg) {
        if (qirQualityAvg <= 50) {
            return "优";
        } else if (qirQualityAvg > 50 && qirQualityAvg <= 100) {
            return "良";
        } else if (qirQualityAvg > 100 && qirQualityAvg <= 150) {
            return "轻度污染";
        } else if (qirQualityAvg > 150 && qirQualityAvg <= 200) {
            return "中度污染";
        } else if (qirQualityAvg > 200 && qirQualityAvg <= 300) {
            return "重度污染";
        } else if (qirQualityAvg > 300) {
            return "严重污染";
        } else {
            return "未获得";
        }
    }

    private static String skyconToDesc(String skycon) {
        switch (skycon) {
            case "CLEAR_DAY":
            case "CLEAR_NIGHT":
                return "晴";
            case "PARTLY_CLOUDY_DAY":
            case "PARTLY_CLOUDY_NIGHT":
                return "多云";
            case "CLOUDY":
                return "阴";
            case "LIGHT_HAZE":
                return "轻度雾霾";
            case "MODERATE_HAZE":
                return "中度雾霾";
            case "HEAVY_HAZE":
                return "重度雾霾";
            case "LIGHT_RAIN":
                return "小雨";
            case "MODERATE_RAIN":
                return "中雨";
            case "HEAVY_RAIN":
                return "大雨";
            case "STORM_RAIN":
                return "暴雨";
            case "FOG":
                return "雾";
            case "LIGHT_SNOW":
                return "小雪";
            case "MODERATE_SNOW":
                return "中雪";
            case "HEAVY_SNOW":
                return "大雪";
            case "STORM_SNOW":
                return "暴雪";
            case "DUST":
                return "浮尘";
            case "SAND":
                return "沙尘";
            case "WIND":
                return "大风";
            default:
                return "未获得";
        }
    }
}
